import {Injectable} from "@angular/core";
import {Http,Headers,RequestOptions} from "@angular/http";
import {Observable} from "rxjs/Observable";
import {User} from "../../app/model/User";
import {Photo} from "../../app/model/Photo";

let headers = new Headers({
    'Content-Type': 'application/json'
});
let options = new RequestOptions({ headers: headers });

@Injectable()
export class RegisterServices{

        constructor(private  http:Http){
        }

        sendUser(user:User){
            let url ="http://localhost:8099/user/register";
            return this.http.post(url,JSON.stringify(user),options);
        }

        sendCredentials(model:any){
            let url ="http://localhost:8099/user/login";
            return this.http.post(url,JSON.stringify(model),options);
        }

        sendToken(token:any){
            let url ="http://localhost:8099/rest/user/users";
            options.headers.append("Authorization","Bearer "+token);
            return this.http.get(url,options);
        }

        checkLogin(){
          if(window.localStorage.getItem("token")!="" && window.localStorage.getItem("currentUserName")!=""){
              return true;
          }else{
            return false;
          }
        }

        loginOut(){
          window.localStorage.setItem("token","");
          window.localStorage.setItem("currentUserName","");
          alert("退出登录");

        }

        getUserByName(param:any){
            options.headers.append("Authorization","Bearer "+window.localStorage.getItem("token"));
            let url = "http://localhost:8099/rest/user/userName";
            return this.http.post(url,param,options);
        }

        getPhotosByUser(user:any){
            options.headers.append("Authorization","Bearer "+window.localStorage.getItem("token"));
            let url = "http://localhost:8099/rest/phtot/user";
            return this.http.post(url,JSON.stringify(user),options);
        }
}
